package com.eibus.applicationconnector.twitter.Utils;

import com.eibus.xml.nom.Find;
import com.eibus.xml.nom.Node;


@SuppressWarnings("deprecation")
public class XMLUtils {

	

    /**
     * Returns a parameter from the XML.
     *
     * @param   rootNode  Root XML node.
     * @param   elemName  Parameter element name.
     *
     * @return  Parameter value.
     *
     * @throws  Exception
     */
    public static boolean getBooleanParameter(int rootNode, String elemName)
                                       throws Exception
    {
        String value = getStringParameter(rootNode, elemName, "false");

        return "true".equals(value);
    }

    /**
     * Returns a parameter from the XML.
     *
     * @param   rootNode   Root XML node.
     * @param   elemName   Parameter element name.
     * @param   mandatory  Thrown if <code>true</code> and the parameter does not exist
     *
     * @return  Parameter value.
     *
     * @throws  Exception
     */
    public static boolean getBooleanParameter(int rootNode, String elemName, boolean mandatory)
                                       throws Exception
    {
        String value = getStringParameter(rootNode, elemName, mandatory);

        return "true".equals(value);
    }

    /**
     * Returns a parameter from the XML.
     *
     * @param   rootNode      Root XML node.
     * @param   elemName      Parameter element name.
     * @param   defaultValue  Value to be returned if the parameter did not exist.
     *
     * @return  Parameter value.
     *
     * @throws  Exception
     */
    public static double getDoubleParameter(int rootNode, String elemName, double defaultValue)
                                     throws Exception
    {
        String value = getStringParameter(rootNode, elemName, Double.toString(defaultValue));

        try
        {
            return Double.parseDouble(value);
        }
        catch (Exception e)
        {
            throw new Exception("Illegal value " + value + " for parameter " + elemName);
        }
    }

    /**
     * Returns a parameter from the XML.
     *
     * @param   rootNode   Root XML node.
     * @param   elemName   Parameter element name.
     * @param   mandatory  Thrown if <code>true</code> and the parameter does not exist
     *
     * @return  Parameter value.
     *
     * @throws  Exception
     */
    public static double getDoubleParameter(int rootNode, String elemName, boolean mandatory)
                                     throws Exception
    {
        String value = getStringParameter(rootNode, elemName, mandatory);

        try
        {
            return Double.parseDouble(value);
        }
        catch (Exception e)
        {
            throw new Exception("Illegal value " + value + " for parameter " + elemName);
        }
    }

    /**
     * Returns a parameter from the XML.
     *
     * @param   rootNode      Root XML node.
     * @param   elemName      Parameter element name.
     * @param   defaultValue  Value to be returned if the parameter did not exist.
     *
     * @return  Parameter value.
     *
     * @throws  Exception
     */
    public static long getLongParameter(int rootNode, String elemName, long defaultValue)
                                 throws Exception
    {
        String value = getStringParameter(rootNode, elemName, Long.toString(defaultValue));

        try
        {
            return Long.parseLong(value);
        }
        catch (Exception e)
        {
            throw new Exception("Illegal value " + value + " for parameter " + elemName);
        }
    }

    /**
     * Returns a parameter from the XML.
     *
     * @param   rootNode   Root XML node.
     * @param   elemName   Parameter element name.
     * @param   mandatory  Thrown if <code>true</code> and the parameter does not exist
     *
     * @return  Parameter value.
     *
     * @throws  Exception
     */
    public static long getLongParameter(int rootNode, String elemName, boolean mandatory)
                                 throws Exception
    {
        String value = getStringParameter(rootNode, elemName, mandatory);

        try
        {
            return Long.parseLong(value);
        }
        catch (Exception e)
        {
            throw new Exception("Illegal value " + value + " for parameter " + elemName);
        }
    }

    /**
     * Returns a parameter from the XML.
     *
     * @param   rootNode      Root XML node.
     * @param   elemName      Parameter element name.
     * @param   defaultValue  Value to be returned if the parameter did not exist.
     *
     * @return  Parameter value.
     */
    public static String getStringParameter(int rootNode, String elemName, String defaultValue)
    {
        int node;
        String value = null;

        if ((node = Find.firstMatch(rootNode, "<><" + elemName + ">")) != 0)
        {
            value = Node.getData(node);
        }

        if (value == null)
        {
            return defaultValue;
        }

        return value;
    }

    /**
     * Returns a parameter from the XML.
     *
     * @param   rootNode   Root XML node.
     * @param   elemName   Parameter element name.
     * @param   mandatory  Thrown if <code>true</code> and the parameter does not exist
     *
     * @return  Parameter value.
     *
     * @throws  Exception
     */
    public static String getStringParameter(int rootNode, String elemName, boolean mandatory)
                                     throws Exception
    {
        int node;
        String value = null;

        if ((node = Find.firstMatch(rootNode, "<><" + elemName + ">")) != 0)
        {
            value = Node.getData(node);
        }

        if (mandatory && ((value == null) || (value.length() == 0)))
        {
            throw new Exception("Parameter " + elemName + " is missing.");
        }

        return value;
    }
}
